//
//  GameDetailTopView.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/9/22.
//  Copyright © 2020 myTV. All rights reserved.
//

#import "GameDetailTopView.h"
#import "GameDetailSetCollectionApi.h"
#import "GameDetailDelCollectionApi.h"
#import "UIImage+Common.h"
#import "CWStarRateView.h"

@interface GameDetailTopView()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgImageHeight;

@property (weak, nonatomic) IBOutlet UIImageView *bgImage;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet YYAnimatedImageView *gameImage;
@property (weak, nonatomic) IBOutlet UILabel *gameName;
@property (weak, nonatomic) IBOutlet UILabel *openTimeLabel;
@property (weak, nonatomic) IBOutlet UIView *signView;

@property (weak, nonatomic) IBOutlet UIView *scoreStarView;
@property (weak, nonatomic) IBOutlet UILabel *hotNumberLabel;

@property (strong, nonatomic) CWStarRateView *starRateView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

@property (weak, nonatomic) IBOutlet UILabel *messageCount0;
@property (weak, nonatomic) IBOutlet UILabel *messageCount1;
@property (weak, nonatomic) IBOutlet UILabel *messageCount2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messageCountWidth0;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messageCountWidth1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messageCountWidth2;

@end

@implementation GameDetailTopView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.bgImageHeight.constant = UIDevice.vg_statusBarHeight + SL_WINDOW_WIDTH *(10.0/16.0);
    self.bottomView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visualView.alpha = 0.9;
    [self.bgImage addSubview:visualView];
    visualView.frame = CGRectMake(0, self.bgImageHeight.constant - 100, SL_WINDOW_WIDTH, 100);
    
    self.starRateView = [[CWStarRateView alloc] initWithFrame:CGRectMake(10, 4, self.scoreStarView.width - 20, 15) numberOfStars:5 openTapGesture:NO];
    self.starRateView.allowIncompleteStar = YES;
    self.starRateView.hasAnimation = NO;
    [self.scoreStarView addSubview:self.starRateView];
}

- (void)setViewModel:(GameDetailNewViewModel *)viewModel
{
    _viewModel = viewModel;
    
    GameDetailNewModel *detail = viewModel.detailModel;
    
    [self.bgImage yy_setImageWithURL:[NSURL URLWithString:detail.gameMainPicture] placeholder:placeholder_image];
    [self.gameImage yy_setImageWithURL:[NSURL URLWithString:detail.gameMainIcon] placeholder:placeholder_image];
    self.gameName.text = [NSString stringWithFormat:@"%@-%@", detail.gameName, detail.gameSubName];
    self.scoreLabel.text = detail.gameScore;
    _starRateView.scorePercent = detail.gameScore.doubleValue / _starRateView.stars * 2;
    
    [self.signView removeAllSubviews];
    UIView *temsignOneView = nil;
    for (NSString *signStr in detail.gameSignArr) {
        UIView *signOneView = [[UIView alloc] init];
        signOneView.backgroundColor = kRGBColor(245, 245, 245);
        signOneView.layer.cornerRadius = 4;
        signOneView.layer.masksToBounds = YES;
        [self.signView addSubview:signOneView];
        UILabel *signLabel = [[UILabel alloc] init];
        signLabel.text = signStr;
        signLabel.font = [UIFont systemFontOfSize:12];
        signLabel.textColor = kColor888888;
        [signOneView addSubview:signLabel];
        [signOneView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.signView).offset(5);
            if (temsignOneView) {
                make.left.equalTo(temsignOneView.mas_right).offset(5);
            } else {
                make.left.equalTo(self.signView).offset(20);
            }
        }];
        [signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(signOneView);
            make.top.equalTo(signOneView).offset(2);
            make.left.equalTo(signOneView).offset(3);
        }];
        temsignOneView = signOneView;
    }
    
    self.messageCount0.text = detail.fanliCount;
    self.messageCount1.text = detail.daijinquanCount;
    self.messageCount2.text = detail.libaoCount;
    self.messageCountWidth0.constant = [self boundingRectWithLabel:self.messageCount0];
    self.messageCountWidth1.constant = [self boundingRectWithLabel:self.messageCount1];
    self.messageCountWidth2.constant = [self boundingRectWithLabel:self.messageCount2];

}

- (CGFloat)boundingRectWithLabel:(UILabel *)label
{
    CGFloat width = [label.text boundingRectWithSize:CGSizeMake(MAXFLOAT, label.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: label.font} context:nil].size.width;
    if (width < 10) {
        width = 10;
    }
    return width + 10;
}

- (IBAction)btnAction:(UIButton *)sender {
    if (sender.tag == 0) {
        NSLog(@"金币支付");
    } else if (sender.tag == 0) {
        NSLog(@"畅玩卡");
    } else if (sender.tag == 0) {
        NSLog(@"可出售");
    }
    
}


+ (CGFloat)height
{
    CGFloat height = UIDevice.vg_statusBarHeight + SL_WINDOW_WIDTH * (10.0 / 16.0) + 80 + 80;
    
    return height;
}



@end
